import requests
import json
import pymysql

url = 'https://www.cingta.com/school/api/name_uni_list/'

response = requests.get(url).text
data = json.loads(response)
news = data['data']['list']

db = pymysql.connect(host="localhost", user="root", password="123456", database="university_data")
cursor = db.cursor()
insert_sql = ''
for n in news:
  code = n['code']
  schoolname = n['schoolname']
  province = n['province']
  city = n['city']
  department = n['department']
  level = n['level']
  type = n['type']
  link = n['link']
  cursor.execute('insert into university(code, schoolname, province, city, department, level, type, link) values(%s,%s,%s,%s,%s,%s,%s,%s)',
                 (str(code), str(schoolname), str(province), str(city), str(department), str(level), str(type), str(link)))
  db.commit()

db.close()

